#encoding=utf-8
from myhomework.account.daos import *
from myhomework.homework.daos import *

"""
def test():
        u1 = User(student_id='001', name='u1')
        UserDao.add_user(u1)
        u2 = User(student_id='002', name='u2')
        UserDao.add_user(u2)
        u3 = User(student_id='003', name='u3')
        UserDao.add_user(u3)
        u4 = User(student_id='004', name='u4')
        UserDao.add_user(u4)
        u5 = User(student_id='005', name='u5')
        UserDao.add_user(u5)
        
        a_user = User.objects.get(student_id='001')
        b_user = User.objects.get(student_id='002')
        c_user = User.objects.get(student_id='005')
        new_group = Group(administrator=a_user, name='g1')
        new_group.save()
        a_user.group = new_group
        b_user.group = new_group
        c_user.group = new_group
        a_user.save()
        b_user.save()
        c_user.save()
 
        c_user = User.objects.get(student_id='003')
        d_user = User.objects.get(student_id='004')
        new_group2 = Group(administrator=c_user, name='g2')
        new_group2.save()
        c_user.group = new_group2
        d_user.group = new_group2
        c_user.save()
        d_user.save()       
        
        h1 = Homework(name='homework1',
                      content='content1',
                      deadline='2011-5-19',
                      type='personal')   
        HomeworkDao.add_homework(h1)
        h2 = Homework(name='homework2',
                      content='content2',
                      deadline='2011-6-19',
                      type='team')   
        HomeworkDao.add_homework(h2)


        user1 = UserDao.get_user_by_student_id('001')
        user2 = UserDao.get_user_by_student_id('002')
        user5 = UserDao.get_user_by_student_id('005')
        group1 = GroupDao.get_group_by_name('g1')
        p_homework = HomeworkDao.get_homework_by_name('homework1')
        t_homework = HomeworkDao.get_homework_by_name('homework2')        
             
        '''
        submit homework
        '''
        list = []
        c1 = Contribution(user=user1, contribution=30)
        c2 = Contribution(user=user2, contribution=70)
        list.append(c1)
        list.append(c2)
    
        group1_ths1 = HomeworkSubmitedInfo(homework=t_homework,
                                    src='src1',
                                    url='url1',
                                    score=70,
                                    rank = 0)
        
        HomeworkSubmitedDao.add_homework_submited(group1_ths1, 
                                                  user1, 
                                                  list)
        
        user1_phs1 = HomeworkSubmitedInfo(homework=p_homework,
                                    src='src1',
                                    url='url1',
                                    score= 0,
                                    rank = 0)
                
        HomeworkSubmitedDao.add_homework_submited(user1_phs1, 
                                                  user1)

        user2_phs1 = HomeworkSubmitedInfo(homework=p_homework,
                                    src='src2',
                                    url='url2',
                                    score= 0,
                                    rank = 0)
                
        HomeworkSubmitedDao.add_homework_submited(user2_phs1, 
                                                  user2)  
        
        user5_phs1 = HomeworkSubmitedInfo(homework=p_homework,
                                    src='src5',
                                    url='url5',
                                    score= 0,
                                    rank = 0)
                
        HomeworkSubmitedDao.add_homework_submited(user5_phs1, 
                                                  user5)  
             
        '''
        assessment the homework
        '''
        ranking_list = []
        ranking_list.append(AssessmentDao.create_ranking_item(user1, 1))
        ranking_list.append(AssessmentDao.create_ranking_item(user2, 2))
        ranking_list.append(AssessmentDao.create_ranking_item(user5, 3))
        AssessmentDao.set_ranking_in_group(p_homework.id, 
                                           group1.id, 
                                           ranking_list)
        am1 = Assessment(homework = p_homework,
                         group = group1,
                         highest_score = 90,
                         lowest_score = 60,
                         ranking_check = True)
        
        am2 = Assessment(homework = t_homework,
                         group = group1,
                         highest_score = 85,
                         lowest_score = 85)

        AssessmentDao.add_assessment(am1)
        AssessmentDao.add_assessment(am2)
        

#test()

#coding=utf-8 

class DownloadDao(object):
    
    @staticmethod
    def make_xls():
        w = Workbook()
        ws_p = w.add_sheet(u'成绩')
        
        ws_p.write(1,0,u'学号')
        ws_p.write(1,1,u'名字')
        
        user_list = UserDao.get_user_list(0,1000)
        i=2
        
        for user in user_list:
            
            ws_p.write(i,0,user.student_id)
            ws_p.write(i,1,user.name)
            
            i = i+1
        
        
        homework_list = HomeworkDao.get_homework_list(0,1000)
        print homework_list
        
        
        i=2
        j=2
        
        
        for homework in homework_list:
            
            ws_p.write(0,i,homework.name)
            ws_p.write(1,i,u'分数')
            ws_p.write(1,i+1,u'排名')
            
            j=2
            
            for user in user_list:
            
                result = AssessmentDao.get_homework_performance(homework.id, user.student_id)
                
                if ( result is None):
                    ws_p.write(j,i,u'缺交')
                    pass
                else:
                    print homework
                    print user
                    print result
                    
                    ws_p.write(j,i,result['mark'])
                    ws_p.write(j,i+1,result['rank'])
               
                j = j+1
            
            
            i = i+2
        
        w.save('score.xls')

Download.make_xls()
"""


from django.http import HttpResponse
from myhomework.pyExcelerator import *
from myhomework.pyExcelerator import CompoundDoc
    
class DownloadView(object):
    
    @staticmethod
    def download_all_score(request,context={}):
        
        """make xls"""
        w = Workbook()
        ws_p = w.add_sheet(u'成绩')
        
        ws_p.write(1,0,u'学号')
        ws_p.write(1,1,u'名字')
        
        user_list = UserDao.get_user_list(0,1000)
        i=2
        
        for user in user_list:
            
            ws_p.write(i,0,user.student_id)
            ws_p.write(i,1,user.name)
            
            i = i+1
        
        
        homework_list = HomeworkDao.get_homework_list(0,1000)
             
        i=2
        j=2
        
        
        for homework in homework_list:
            
            ws_p.write(0,i,homework.name)
            ws_p.write(1,i,u'分数')
            ws_p.write(1,i+1,u'排名')
            
            j=2
            
            for user in user_list:
            
                result = AssessmentDao.get_homework_performance(homework.id, user.student_id)
                
                if ( result is None):
                    ws_p.write(j,i,u'缺交')
                 
                else:
                    ws_p.write(j,i,result['mark'])
                    ws_p.write(j,i+1,result['rank'])
               
                j = j+1
            
            
            i = i+2
    
        """make xls"""
        
        response = HttpResponse(mimetype='application/vnd.ms-excel')
        response['Content-Disposition'] = 'attachment; filename=all_sscore.xls'
        
        response.write(w.savestream())
        
        return response
    
    @staticmethod
    def download_personal_score(request,context={}):
        
        student_id = '001'
        #student_id = request.user.username
        user = UserDao.get_user_by_student_id(student_id);
        
        """make xls"""
        w = Workbook()
        ws_p = w.add_sheet(u'成绩')
        
        ws_p.write(0,0,u'学号')
        ws_p.write(1,0,user.student_id);
        
        ws_p.write(0,1,u'姓名')
        ws_p.write(1,1,user.name)
        
        ws_p.write(0,2,u'小组')
        ws_p.write(1,2,user.group.name)
        
        homework_list = HomeworkDao.get_homework_list(0,1000)

        i=3
        
        for homework in homework_list:
            
            ws_p.write(i,0,homework.name)
            
            ws_p.write(i,4,u'分数')
            ws_p.write(i,5,u'排名')
                
            result = AssessmentDao.get_homework_performance(homework.id, user.student_id)
                    
            if ( result is None):
                ws_p.write(i+1,4,u'缺交')
                  
            else:   
                ws_p.write(i+1,4,result['mark'])
                ws_p.write(i+1,5,result['rank'])
            
            if homework.type == 'personal':

                group = user.group
                count = 0   
                
                ws_p.write(i,2,u'小组评审结果')
                ws_p.write(i+1,2,u'姓名')
                ws_p.write(i+1,3,u'排名') 
                
                group_judge = AssessmentDao.get_rankings_in_group(homework.id, user.group.id)
                
                    
                for member in group_judge:
                    
                    ws_p.write(i+2+count,2,member.name)
                    ws_p.write(i+2+count,3,count+1)
                    
                    count = count + 1
                    
                i = i + 3 +count
            
            else:
                
                ws_p.write(i,2,u'小组作业')
                i = i + 3 
                
         
        """make xls"""
        
        response = HttpResponse(mimetype='application/vnd.ms-excel')
        response['Content-Disposition'] = 'attachment; filename=personal_score.xls'
        
        response.write(w.savestream())
        
        return response
    